﻿package {
	
	import flash.display.MovieClip;
	import flash.text.*;
	import flash.events.*;
	
	public class ImageProperty extends MovieClip {
		
		private var image:ImageObject;
		
		private var xSelect:NumberSelectBox;
		private var ySelect:NumberSelectBox;
		private var scaleXSelect:NumberSelectBox;
		private var scaleYSelect:NumberSelectBox;	
		
		public function ImageProperty(img:ImageObject = null) {
			image = img;
			setupPositioning();
		}
		
		//remove attachment from current image
		public function dislocate() { image = null; }
		
		public function getKeyValue(key:String):String {
			switch(key) {
				case "x":
					return String(xSelect.val);
				case "y":
					return String(ySelect.val);
				case "scalex":
					return String(scaleXSelect.val);
				case "scaley":
					return String(scaleYSelect.val);
				default:
					return "null";
			}
		}
		
		public function configure(img:ImageObject) {
			image = img
			xSelect.setSize(image.getProp("x"));
			ySelect.setSize(image.getProp("y"));
			scaleXSelect.setSize(image.getProp("scalex"));
			scaleYSelect.setSize(image.getProp("scaley"));
		}
		
		private function changeX(e:Event) {
			if (image == null) return;
			image.setProp("x", xSelect.val)
		}
		
		private function changeY(e:Event) {
			if (image == null) return;
			image.setProp("y", ySelect.val)
		}
		
		private function changeScaleX(e:Event) {
			if (image == null) return;
			image.setProp("scalex", scaleXSelect.val)
		}
		
		private function changeScaleY(e:Event) {
			if (image == null) return;
			image.setProp("scaley", scaleYSelect.val)
		}
		
		private function setupPositioning() {
			xSelect = new NumberSelectBox(135, 40, 30, 20, 1024, 100, 5, 100);
			ySelect = new NumberSelectBox(180, 40, 30, 20, 768, 100, 5, 100);
			scaleXSelect = new NumberSelectBox(225, 40, 30, 20, 10, 0.05, 0.05, 100);
			scaleYSelect = new NumberSelectBox(270, 40, 30, 20, 10, 0.05, 0.05, 100);
			addChild(createLabel("X:", 120, 40, 20, 20));
			addChild(xSelect.input);
			addChild(createLabel("Y:", 165, 40, 20, 20));
			addChild(ySelect.input);
			addChild(createLabel("SX:", 210, 40, 20, 20));
			addChild(scaleXSelect.input);
			addChild(createLabel("SY:", 255, 40, 20, 20));
			addChild(scaleYSelect.input);
			xSelect.addEventListener(Event.CHANGE, changeX);
			ySelect.addEventListener(Event.CHANGE, changeY);
			scaleXSelect.addEventListener(Event.CHANGE, changeScaleX);
			scaleYSelect.addEventListener(Event.CHANGE, changeScaleY);
		}
		
		private function createLabel(str:String, X:int, Y:int, W:int, H:int, color:uint = 0x000000):TextField {
			var tf:TextField = new TextField();
			tf.text = str;
			tf.x = X; tf.y = Y; tf.width = W; tf.height = H;
			var format:TextFormat = new TextFormat();
			format.color = color;
			tf.setTextFormat(format);
			return tf;			
		}
	}
}